<?php

namespace App\Http\Controllers\Admin;
use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ArticleCategoryRequest;
use App\Models\ArticleCategory;
use App\Models\Department;
use App\Models\DepartmentHasPermission;
use App\Models\DepartmentPermission;
use App\Models\ModelHasPermission;
use App\Models\RoleHasPermission;
use App\Models\SystemUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;

class DepartmentPermissionController extends Controller
{
    use ResponseData;
    public $array = [];
    public function list(DepartmentPermission $model,Request $request){
        $size = $request->input('size',10);
        if($request->filled('level')){
            $model = $model->where('level',$request->level);
        }
        if($request->filled('name')){
            $model = $model->where('name','like','%'.$request->name.'%');
        }
        if($request->filled('status')&&$request->status=='all'){
            return $this->success($model->get()) ;
        }else{
            $model = $model->paginate($size);
            $data = $this->getGradeList($model);
            return $this->success($data);
        }
    }
    protected function getGradeList($data){
        foreach ($data as $k=>$v){
            $arr = explode(',',$v->pid);
            $array = [];
            foreach ($arr as $m=>$n){
                $res = $this->getData($n);
                if($res){
                    $array[$m] = $res;
                }
            }
            $data[$k]['pid_list'] = $array;
        }
        return $data;
    }
    protected function getData($id){
        return ArticleCategory::query()->find($id);
    }
    //获取交集 删除部门权限
    protected function inerect($left,$right,$department_id){
        //获取交集
        $intersect = array_intersect($left,$right);

        //获取被删除的
        $delete = array_diff($left,$intersect);
        sort($delete);

        if(count($delete)>0){
            //删除部门权限
            $this->delDepartmentPermission($department_id,$delete);
            //删除角色权限
           $this->delRolePermission($department_id,$delete);
            //删除用户权限
            $this->delUserPermission($department_id,$delete);
        }

        //获取需要新增的
        $add = array_diff($right,$intersect);
        if(count($add)>0){
            $this->addPermission($add,$department_id);
        }

    }
    //删除部门权限
    protected function delDepartmentPermission($department_id,$delPermission){
        $department = Department::query()->find($department_id);

        if($department){
            if($department->level==1){
                //查出下一等级的id
                $arr = $this->getChildDepartmentId($department_id);
                if(count($arr)>0){
                    foreach ($arr as $v){
                        $this->delAccDepartmentPermission($v,$delPermission);
                        $map = $this->getChildDepartmentId($v);
                        if(count($map)>0){
                            foreach ($map as $item){
                                $this->delAccDepartmentPermission($v.','.$item,$delPermission);
                            }
                        }
                    }
                }
            }
            if($department->level==2){
                //查出下一等级的id
                $arr = $this->getChildDepartmentId($department_id);
                if(count($arr)>0){
                    foreach ($arr as $v){
                        $this->delAccDepartmentPermission($department->pid.','.$v,$delPermission);
                    }
                }
            }
           $this->delAccDepartmentPermission($department_id,$delPermission);
        }
    }
    //获取下级部门id
    protected function getChildDepartmentId($department_id){
        return Department::query()->where('pid',$department_id)->pluck('id');
    }
    //删除部门权限
    protected function delAccDepartmentPermission($department_id,$delPermission){
        DepartmentHasPermission::query()->where('department_id',$department_id)->whereIn('permission_id',$delPermission)->delete();
    }
    //删除角色权限
    protected function delRolePermission($department_id,$delPermission){
        $role_id = Role::query()->where('department_id',$department_id)->pluck('id')->toArray();
        if($role_id&&count($role_id)>0){
            foreach ($role_id as $v){
                //循环删除权限
                RoleHasPermission::query()->where('role_id',$v)->whereIn('permission_id',$delPermission)->delete();
            }
        }
    }
    //删除用户的权限
    protected function delUserPermission($department_id,$delPermission){
        $user_id = SystemUser::query()->where('department_id',$department_id)->pluck('id')->toArray();
        if(is_array($user_id)&&count($user_id)>0){
            foreach ($user_id as $v){
                //循环删除权限
                ModelHasPermission::query()->where('model_id',$v)->whereIn('permission_id',$delPermission)->delete();
            }
        }
    }
    public function store(DepartmentHasPermission $model,Request $request){
        if(!$request->filled('id')){
            return $this->failed('参数错误',201);
        }
        //获取原来的权限
        $left = $model->query()->where('department_id',$request->id)->pluck('permission_id')->toArray();

      //  $model->query()->where('department_id',$request->id)->delete();
        //获取到传递的参数
        $arr = $this->getArray($request->permission);
//        //获取交集

        if(count($left)>0&&count($arr)>0){

           $this->inerect($left,$arr,$request->id);
        }else{
            foreach($arr as $k=>$v){
                $res = DepartmentHasPermission::query()
                    ->where('permission_id',$v)
                    ->where('department_id',$request->id)->first();
                if(!$res){
                    DepartmentHasPermission::query()->create(['permission_id'=>$v,'department_id'=>$request->id]);
                }
            }
        }
        return $this->success('保存成功');
    }
    protected function addPermission($arr,$id){
        foreach ($arr as $k=>$v){
            $data[$k]['department_id'] = $id;
            $data[$k]['permission_id'] = $v;
        }
        if(!empty($data)){
            DB::table('department_has_permissions')->insert($data);
        }
    }
    protected function getArray($data){
        if(is_array($data)){
            return $data;
        }else{
            return json_decode($data,true);
        }
    }
    //部门拥有权限
    public function DepartmentOwnerPermission(Department $model,Request $request){
        $data = $model->query()->with(['permissions'=>function($query){
            $query->select('id','route','zh_name','name','module_name','sort','icon','guard_name','type');
        }])->find($request->id);
        return $this->success($data);
    }
}
